#include "mywebengineview.h"
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))

#include <QChildEvent>
#include <QPointer>
#include <QOpenGLWidget>
#include <QWebEngineHistory>
#include <QWebEnginePage>
#include <QMouseEvent>
#include <QApplication>
#include <QTouchEvent>
#include "cx_logger.h"

MyWebEngineView::MyWebEngineView(QWidget *parent)
    : QWebEngineView(parent)
    ,child_(nullptr)
    ,m_bIsArrowTool(true)
{
    this->setAttribute(Qt::WA_AcceptTouchEvents);
}

bool MyWebEngineView::eventFilter(QObject *obj, QEvent *ev)
{
    // emit delegatePaint on paint event of the last added QOpenGLWidget child
    if (obj == child_ && ev->type() == QEvent::Paint)
    {
        QPaintEvent *pe = static_cast<QPaintEvent*>(ev);
        // do something with paint event
        // ...
        // or just emit signal to notify other objects
        //emit delegatePaint(pe);
    }
    else if (obj == child_ &&
             (ev->type() == QEvent::MouseButtonDblClick
              || ev->type() == QEvent::MouseButtonPress
              || ev->type() == QEvent::MouseButtonRelease
              || ev->type() == QEvent::MouseMove
              || ev->type() == QEvent::ContextMenu))
    {
       // QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(ev);
        QApplication::instance()->sendEvent(this, ev);
    }
    else if (!m_bIsArrowTool)
    {
        if (obj == child_ && ev->type() == QEvent::TouchBegin)
        {
            QTouchEvent *touchEvent = static_cast<QTouchEvent *>(ev);
            QMouseEvent *mouseEvent = new QMouseEvent(QEvent::MouseButtonPress,
                                                      touchEvent->touchPoints().first().pos(),
                                                      Qt::LeftButton,
                                                      Qt::LeftButton,
                                                      Qt::NoModifier);
            emit eventTransit(mouseEvent);
            return true;
        }
        else if (obj == child_ && ev->type() == QEvent::TouchUpdate)
        {
            QTouchEvent *touchEvent = static_cast<QTouchEvent *>(ev);
            QMouseEvent *mouseEvent = new QMouseEvent(QEvent::MouseMove,
                                         touchEvent->touchPoints().first().pos(),
                                         Qt::LeftButton,
                                         Qt::NoButton,
                                         Qt::NoModifier);
            emit eventTransit(mouseEvent);
            return true;
        }
        else if (obj == child_ && ev->type() == QEvent::TouchEnd)
        {
            QTouchEvent *touchEvent = static_cast<QTouchEvent *>(ev);
            QMouseEvent *mouseEvent = new QMouseEvent(QEvent::MouseButtonRelease,
                                                      touchEvent->touchPoints().first().pos(),
                                                      Qt::LeftButton,
                                                      Qt::LeftButton,
                                                      Qt::NoModifier);
            emit eventTransit(mouseEvent);
            return true;
        }
    }
    return QWebEngineView::eventFilter(obj, ev);
}

bool MyWebEngineView::event(QEvent * ev)
{
    if (ev->type() == QEvent::ChildAdded) {
        QChildEvent *child_ev = static_cast<QChildEvent*>(ev);

        // there is also QObject child that should be ignored here;
        // use only QOpenGLWidget child
        QOpenGLWidget *w = qobject_cast<QOpenGLWidget*>(child_ev->child());
        if (w)
        {
            child_ = w;
            w->installEventFilter(this);
        }
    }    
    return QWebEngineView::event(ev);
}

QWebEngineView *MyWebEngineView::createWindow(QWebEnginePage::WebWindowType type)
{
    if (type == QWebEnginePage::WebBrowserTab)
    {
        return this;
    }
    return QWebEngineView::createWindow(type);
}
#endif
